﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from Entities import *
#---------------------------
# Entities TrangThaiXetDuyet
#---------------------------    
def Get(keyNguoiDung, TrangThaiXetDuyetID = 0,TenTrangThai=""):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    results = TrangThaiXetDuyet.all()
    trangthai_list = []
    if TenTrangThai != "":
        results = results.filter("TenTrangThai =", unicode(TenTrangThai))
    if TrangThaiXetDuyetID > 0:
        trangthai = TrangThaiXetDuyet.get_by_id(TrangThaiXetDuyetID)
        trangthai_dict ={}
        trangthai_dict['TrangThaiID'] = trangthai.key().id()
        trangthai_dict['TenTrangThai'] = trangthai.TenTrangThai   
        trangthai_list.append(trangthai_dict)
    else:
        #trangthais = TrangThaiXetDuyet.all().filter("NguoiDung =", nguoidung)
        #results = TrangThaiXetDuyet.all()
        for trangthai in results:
            trangthai_dict ={}
            trangthai_dict['TrangThaiID'] = trangthai.key().id()
            trangthai_dict['TenTrangThai'] = trangthai.TenTrangThai   
            trangthai_list.append(trangthai_dict)
    tuple_trangthai = tuple(trangthai_list)  
    return tuple_trangthai

def All():
    trangthai_list = []
    trangthais = TrangThaiXetDuyet.all()
    for trangthai in trangthais:
        trangthai_dict ={}
        trangthai_dict['TrangThaiID'] = trangthai.key().id()
        trangthai_dict['TenTrangThai'] = trangthai.TenTrangThai   
        trangthai_list.append(trangthai_dict)
    tuple_trangthai = tuple(trangthai_list)  
    return tuple_trangthai

def Add(TenTrangThai):
    trangthai = db.GqlQuery("SELECT * FROM TrangThaiXetDuyet WHERE TenTrangThai = :1",
                              TenTrangThai)
    if trangthai.count() == 0:                      
        trangthai = TrangThaiXetDuyet(TenTrangThai=TenTrangThai)
        trangthai.put()         
        return "Complete"
    else:
        return "Duplicate"

def Update(keyNguoiDung, TrangThaiXetDuyetID, TenTrangThai):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    trangthai_update = TrangThaiXetDuyet.get_by_id(TrangThaiXetDuyetID)
    trangthai = db.GqlQuery("SELECT * FROM TrangThaiXetDuyet WHERE TenTrangThaiXetDuyet = :1 AND NguoiDung = :2",
                              TenTrangThai, nguoidung)
    if trangthai.count() == 0:
        trangthai_update.TenTrangThai = TenTrangThai
        trangthai_update.put()
        return "Complete"
    else:
        return "Duplicate"

def GetID(keyNguoiDung,TenTrangThai):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    trangthais = db.GqlQuery("SELECT * FROM TrangThaiXetDuyet WHERE TenTrangThai = :1",
                               TenTrangThai)

    if trangthais.count() != 0:
        for trangthai in trangthais:
            TrangThaiID = trangthai.key().id()     
    else:
        TrangThaiID = None
    return TrangThaiID
